<template>
  <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
    <el-form-item label="姓名" prop="userName">
    <el-input type="text" v-model="ruleForm.userName" autocomplete="off"></el-input>
  </el-form-item>
  <el-form-item label="密码" prop="userPwd">
    <el-input type="password" v-model="ruleForm.userPwd" autocomplete="off"></el-input>
  </el-form-item>
    <el-button type="primary" @click="submitForm('ruleForm')">登陆</el-button>
    <el-button @click="resetForm('ruleForm')">注册</el-button>
</el-form>
</template>

<script>
import {login} from '../services/UserService.js'
export default {
    data() {
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (this.ruleForm.userPwd !== '') {
            // this.$refs.ruleForm.validateField('userPwd');
          }
          callback();
        }
      };
      var validateUserName = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入姓名'));
        } else {
          callback();
        }
      };
      return {
        ruleForm: {
          userName: '',
          userPwd: '',
        },
        rules: {
          userPwd: [
            { validator: validatePass, trigger: 'blur' }
          ],
          userName: [
            { validator: validateUserName, trigger: 'blur' }
          ],
        }
      };
    },
    methods: {
        submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
              this.$store.dispatch("loginUser/login",this.ruleForm).then((resp)=>{
              if(resp){
                console.log("------------------------");
                console.log(resp);
                this.$router.push({name:"Store",params:{
                  uu:resp
                }})
              }
              else{
                alert("用户名或者密码错误！！！！！！！！！！");
              }
            });
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
    }
  }

</script>

<style>

</style>